package cn.buk.tms.eotms.service;

import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.eotms.dao.PnrDao;
import cn.buk.tms.eotms.entity.Pnr;
import cn.buk.tms.eotms.entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PnrServiceImpl extends BaseAbstractService implements PnrService {

	private final static Logger logger = Logger.getLogger(PnrServiceImpl.class);

	@Autowired
	private PnrDao pnrDao;


	/**
	 * 查找PNR
	 * @param userId
	 * @param sc
	 * @return
	 */
	@Override
  public List<Pnr> searchPnr(final int userId, CommonSearchCriteria sc) {
		final int ownerId = getOwnerIdByUserId(userId);
		final int customerId = getCustomerIdByUserId(userId);

		//根据用户的etermUsername进行查询
		User user = userDao.getUserById(userId);
		String[] etermUserNames = user.getEtermUsername().length() == 0 ? user.getEmpname().split(",") : user.getEtermUsername().split(",");
		if (etermUserNames == null || etermUserNames.length == 0) {
			return new ArrayList<>();
		}

		return pnrDao.search(ownerId, sc, etermUserNames);
	}


	@Override
  public Pnr getPnrById(int id, int enterpriseId) {
		Pnr pnr = pnrDao.getPnrById(id);
		if (pnr != null && pnr.getEnterpriseId() == enterpriseId) {
      return pnr;
    } else {
      return null;
    }
	}

}
